package com.erdos.core.web;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.collections.map.LinkedMap;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.ServletActionContext;

import com.erdos.common.web.BaseAction;
import com.erdos.common.web.LoginUtil;
import com.erdos.core.entity.Attribute;
import com.erdos.core.entity.FieldDefinition;
import com.erdos.core.service.AttributeService;
import com.erdos.core.service.FieldDefinitionService;

public class AttributeAction extends BaseAction<Attribute>{

	private AttributeService attributeService;
	private FieldDefinitionService fieldDefinitionService;
	//Spring注入的Service层接口
	private static final Log logger = LogFactory.getLog(AttributeAction.class);
	//设置日志对象
	private Map<Long,String> facilityMap;
	private Map<String,String> objectMap;
	//页面查询form对象
	private String objectValue;
	private String objectDis;
	private String facilityValue;
	private String facilityDis;
	private String versionFlag;
	private String radiobutton;
	//已有分组列表
	private Map<String,String> facilityGroupMap;
	private List<Long> existFieldsList;
	private Map<Long, String> allFieldsMap;
	//更新用属性
	private String defaultValue;
	private String defaultGroup;
	private String defaultDiscription;
	private String defaultSID;
	private String feildID;
	//状态变量
	private boolean statement;
	//为真时为更新页面，为假时为新增页面。
	/**
	 * @return the feildID
	 */
	public String getFeildID() {
		return feildID;
	}
	/**
	 * @param feildID the feildID to set
	 */
	public void setFeildID(String feildID) {
		this.feildID = feildID;
	}
	/**
	 * 点击菜单后申请的入口Action，初始化页面显示，并加载页面中指定的类表值。
	 */
	public String index() throws Exception{
		
		Map<Long,String> facilityMap = this.attributeService.findFacilityMap();
		//查询工厂combox中的数据
		this.setFacilityMap(facilityMap);
		//用查询结果设置工厂combox值
		Map<String,String> objectMap= this.attributeService.findObjectMap();
		this.setObjectMap(objectMap);
		
		return SUCCESS;
	}
	/**
	 * 根据条件查询结果并显示
	 * @return
	 * @throws Exception
	 * @author Owner
	 * @since  2008-7-23
	 */
	public String listCheck() throws Exception{
		logger.debug("listCheck Action execute!");
		//获取包含页面中文本框中显示的参数
		HttpServletRequest request = ServletActionContext.getRequest();
		this.versionFlag = (String)request.getParameter("versionFlag");
		
		//获取查询条件
		Map<String,Object> paramMap = new HashMap<String,Object>();
		if(objectValue != null){
			paramMap.put("object", objectValue);
		}
		if(facilityValue != null){
			paramMap.put("facilityId", facilityValue);
		}
		if(this.versionFlag !=null){
			paramMap.put("versionFlag", versionFlag);
		}
		if(paramMap!=null){
			//设置查询结果
			this.entities=attributeService.findByParams(paramMap);
		}
		Map<Long,FieldDefinition> fieldDefinitionMap = new HashMap<Long,FieldDefinition>();
		//设置属性显示值为属性名称
		List<FieldDefinition> allFieldsPojo = fieldDefinitionService.findAll();
		for(FieldDefinition tempFieldDefinition :allFieldsPojo){
			fieldDefinitionMap.put(tempFieldDefinition.getSid(), tempFieldDefinition);
		}
		for(Attribute attribute : entities){
			attribute.setFieldDefinition(fieldDefinitionMap.get(attribute.getFieldSid()));
		}
		//设置刷新DIV URL地址
		String query = request.getQueryString();
		String pageURL = request.getServletPath();
		if (query != null && query.length() > 0) {
			pageURL += "?" + query;
		}
		request.setAttribute("contextURL", URLEncoder.encode(pageURL, "UTF-8"));
		logger.debug("pageURL=" + request.getContextPath() + pageURL);
		request.setAttribute("pageURL", request.getContextPath() + pageURL);
		return SUCCESS;
	}

	/**
	 * 新增记录弹出窗口访问Action
	 * @return
	 * @author Owner
	 * @since  2008-7-23
	 */
	public String insertDisplay(){
		
		logger.debug("执行 insertDisplay Action!");
		//获取Param参数并为bean对象属性重新赋值
		HttpServletRequest request = ServletActionContext.getRequest();
		this.facilityDis = request.getParameter("fasDis");
		this.objectDis = request.getParameter("objDis");
		this.objectValue = request.getParameter("objDisID");
		this.facilityValue = request.getParameter("fasDisID");
		this.versionFlag = request.getParameter("versionFlag");
		//获取已有的下拉列表信息
		facilityGroupMap = attributeService.findGroupMap(objectValue,facilityValue);
		//existFields 集合
		if(existFieldsList!=null){
			existFieldsList.clear();
		}
		existFieldsList = attributeService.findExistFieldSids(objectValue,facilityValue);
		//获取所有Field集合
		List<FieldDefinition> allFieldsPojo = fieldDefinitionService.findAll();
		this.allFieldsMap = new LinkedMap();
		for(FieldDefinition fieldDefinitionpojo : allFieldsPojo){
			//便利获取属性集合中的标示与属性值
			allFieldsMap.put(fieldDefinitionpojo.getSid(), fieldDefinitionpojo.getFieldId());
		}
		//判断所有属性中是否包含已选择属性并标示
		for(Object sid: existFieldsList){
			//如果存在已选择属性则修改标示
			String feildID = (String)allFieldsMap.get((Long)sid);
			allFieldsMap.remove((Long)sid);
			if(sid!=null && feildID!=null){
//				allFieldsMap.put((Long)sid, "[已配置]"+feildID);
				allFieldsMap.put((Long)sid, getText("tips.attribute.add.haved") + feildID);
			}	
		}
		statement = false;
		//更新属性设置状态为录入状态，用于页面显示调整。
		return SUCCESS;
	}
	
	/**
	 * 修改记录Action修改选中记录
	 * @throws UnsupportedEncodingException 
	 */
	public String updateDisplay() throws UnsupportedEncodingException{
		
		logger.debug("执行 insertDisplay Action!");
		//获取Param参数并为bean对象属性重新赋值
		HttpServletRequest request = ServletActionContext.getRequest();
		this.facilityDis = request.getParameter("fasDis");
		this.objectDis = request.getParameter("objDis");
		this.objectValue = request.getParameter("objDisID");
		this.facilityValue = request.getParameter("fasDisID");
		this.versionFlag = request.getParameter("versionFlag");
		//获取已有的下拉列表信息
		this.facilityValue = request.getParameter("fasDisID");
		//获取已有的下拉列表信息
		facilityGroupMap = attributeService.findGroupMap(objectValue, facilityValue);
		//existFields 集合
		existFieldsList = attributeService.findExistFieldSids(objectValue,facilityValue);
		
		//为页面显示现有属性赋默认值
		this.defaultDiscription = request.getParameter("attrDis");
		this.defaultGroup = request.getParameter("defaultGroup");
		this.defaultSID = request.getParameter("sid");
		this.defaultValue = request.getParameter("defaultValue");
		this.feildID = request.getParameter("fieldSid");
		//获取所有Field集合
		List<FieldDefinition> allFieldsPojo = fieldDefinitionService.findAll();
		this.allFieldsMap = new LinkedMap();
		for(FieldDefinition fieldDefinitionpojo : allFieldsPojo){
			//便利获取属性集合中的标示与属性值
			if(feildID!=null && !feildID.trim().equals("") && fieldDefinitionpojo.getSid() == Long.parseLong(feildID)){
				allFieldsMap.put(fieldDefinitionpojo.getSid(), fieldDefinitionpojo.getFieldId());
				break;
			}else{
				continue;
			}
		}
		statement = true;
		//更新属性设置状态为更新状态，用于页面显示调整。
		return SUCCESS;
	}
	
	/**
	 * 保存新记录
	 * @throws Exception 
	 */
	public String insert() throws Exception{
		
		HttpServletRequest request = ServletActionContext.getRequest();
		//为当前需要持久化的对象提供分组信息的选择
		this.chooesAttributGroup();
		//提供创建者信息
		this.entity.setCreatedBy(LoginUtil.getUpcUserPin());
		//将对象持久化到数据库中
		attributeService.insert(entity);
		//根据当前查询条件刷新页面显示
		request.setAttribute("method", "insert");
		saveBizMessage(getText("attribute.insert.success"));
		return SUCCESS;
	}
	
	/**
	 * 更新方法
	 */
	public String update(){
		//按照ID查询数据
		HttpServletRequest request = ServletActionContext.getRequest();
		//为当前需要持久化的对象提供分组信息的选择
		this.chooesAttributGroup();
		//提供更新者信息
		this.entity.setUpdatedBy(LoginUtil.getUpcUserPin());
		//将对象持久化到数据库中
		attributeService.update(this.entity);
		request.setAttribute("method", "update");
		saveBizMessage(getText("attribute.update.success"));
		return SUCCESS;
	}
	
	/**
	 * 删除方法
	 */
	public String delete(){
		//删除ID对应的对象
		HttpServletRequest request = ServletActionContext.getRequest();
		Long keyID = this.entity.getSid();
		logger.debug("删除方法执行，删除ID为："+keyID+" 记录！");
		attributeService.deleteByPrimaryKey(keyID);
		request.setAttribute("method", "delete");
		saveBizMessage(getText("attribute.delete.success"));
		return SUCCESS;
	}
	
	/**
	 * 为属性分组选择提交信息。
	 * 
	 * @author Administrator
	 * @since  2008-7-27
	 */
	public void chooesAttributGroup(){
		
		//分组采取选择加填写方式，如果填写了文本框则分组项会有两个值，“，”分隔符，故此处确认使用哪个分组。
		String attributeGroup[]=this.entity.getAttributeGroup().split(",");
//		if(attributeGroup != null && attributeGroup.length > 1 ){
//			//此处判断为需要进行重新赋值操作
//			if(attributeGroup[0].equals("header")){
//				this.entity.setAttributeGroup(attributeGroup[1].trim());
//				//如果第一个选项为首选项，既下拉框选项为默认无选择，采用文本框的选项
//			}else{
//				if(!attributeGroup[0].equals("header") && !attributeGroup[1].trim().equals("")){
//					//如果两项同时符合填写规范则认为文本框中的值为属性组名
//					this.entity.setAttributeGroup(attributeGroup[1].trim());
//				}else{
//					this.entity.setAttributeGroup(attributeGroup[0].trim());
//				}
//			}
//		}
		this.entity.setAttributeGroup(attributeGroup[1].trim());
	}
	/**
	 * @return the attributeService
	 */
	public AttributeService getAttributeService() {
		return attributeService;
	}
	/**
	 * @param attributeService the attributeService to set
	 */
	public void setAttributeService(AttributeService attributeService) {
		this.attributeService = attributeService;
	}
	/**
	 * @return the fieldDefinitionService
	 */
	public FieldDefinitionService getFieldDefinitionService() {
		return fieldDefinitionService;
	}
	/**
	 * @param fieldDefinitionService the fieldDefinitionService to set
	 */
	public void setFieldDefinitionService(
			FieldDefinitionService fieldDefinitionService) {
		this.fieldDefinitionService = fieldDefinitionService;
	}
	/**
	 * @return the facilityMap
	 */
	public Map<Long, String> getFacilityMap() {
		return facilityMap;
	}
	/**
	 * @param facilityMap the facilityMap to set
	 */
	public void setFacilityMap(Map<Long, String> facilityMap) {
		this.facilityMap = facilityMap;
	}
	/**
	 * @return the objectMap
	 */
	public Map<String, String> getObjectMap() {
		return objectMap;
	}
	/**
	 * @param objectMap the objectMap to set
	 */
	public void setObjectMap(Map<String, String> objectMap) {
		this.objectMap = objectMap;
	}
	/**
	 * @return the objectValue
	 */
	public String getObjectValue() {
		return objectValue;
	}
	/**
	 * @param objectValue the objectValue to set
	 */
	public void setObjectValue(String objectValue) {
		this.objectValue = objectValue;
	}
	/**
	 * @return the objectDis
	 */
	public String getObjectDis() {
		return objectDis;
	}
	/**
	 * @param objectDis the objectDis to set
	 */
	public void setObjectDis(String objectDis) {
		this.objectDis = objectDis;
	}
	/**
	 * @return the facilityValue
	 */
	public String getFacilityValue() {
		return facilityValue;
	}
	/**
	 * @param facilityValue the facilityValue to set
	 */
	public void setFacilityValue(String facilityValue) {
		this.facilityValue = facilityValue;
	}
	/**
	 * @return the facilityDis
	 */
	public String getFacilityDis() {
		return facilityDis;
	}
	/**
	 * @param facilityDis the facilityDis to set
	 */
	public void setFacilityDis(String facilityDis) {
		this.facilityDis = facilityDis;
	}
	/**
	 * @return the versionFlag
	 */
	public String getVersionFlag() {
		return versionFlag;
	}
	/**
	 * @param versionFlag the versionFlag to set
	 */
	public void setVersionFlag(String versionFlag) {
		this.versionFlag = versionFlag;
	}
	/**
	 * @return the radiobutton
	 */
	public String getRadiobutton() {
		return radiobutton;
	}
	/**
	 * @param radiobutton the radiobutton to set
	 */
	public void setRadiobutton(String radiobutton) {
		this.radiobutton = radiobutton;
	}
	/**
	 * @return the facilityGroupMap
	 */
	public Map<String, String> getFacilityGroupMap() {
		return facilityGroupMap;
	}
	/**
	 * @param facilityGroupMap the facilityGroupMap to set
	 */
	public void setFacilityGroupMap(Map<String, String> facilityGroupMap) {
		this.facilityGroupMap = facilityGroupMap;
	}
	/**
	 * @return the existFieldsList
	 */
	public List<Long> getExistFieldsList() {
		return existFieldsList;
	}
	/**
	 * @param existFieldsList the existFieldsList to set
	 */
	public void setExistFieldsList(List<Long> existFieldsList) {
		this.existFieldsList = existFieldsList;
	}
	/**
	 * @return the allFieldsMap
	 */
	public Map<Long, String> getAllFieldsMap() {
		return allFieldsMap;
	}
	/**
	 * @param allFieldsMap the allFieldsMap to set
	 */
	public void setAllFieldsMap(Map<Long, String> allFieldsMap) {
		this.allFieldsMap = allFieldsMap;
	}
	/**
	 * @return the defaultValue
	 */
	public String getDefaultValue() {
		return defaultValue;
	}
	/**
	 * @param defaultValue the defaultValue to set
	 */
	public void setDefaultValue(String defaultValue) {
		this.defaultValue = defaultValue;
	}
	/**
	 * @return the defaultGroup
	 */
	public String getDefaultGroup() {
		return defaultGroup;
	}
	/**
	 * @param defaultGroup the defaultGroup to set
	 */
	public void setDefaultGroup(String defaultGroup) {
		this.defaultGroup = defaultGroup;
	}
	/**
	 * @return the defaultDiscription
	 */
	public String getDefaultDiscription() {
		return defaultDiscription;
	}
	/**
	 * @param defaultDiscription the defaultDiscription to set
	 */
	public void setDefaultDiscription(String defaultDiscription) {
		this.defaultDiscription = defaultDiscription;
	}
	/**
	 * @return the defaultSID
	 */
	public String getDefaultSID() {
		return defaultSID;
	}
	/**
	 * @param defaultSID the defaultSID to set
	 */
	public void setDefaultSID(String defaultSID) {
		this.defaultSID = defaultSID;
	}
	/**
	 * @return the statement
	 */
	public boolean isStatement() {
		return statement;
	}
	/**
	 * @param statement the statement to set
	 */
	public void setStatement(boolean statement) {
		this.statement = statement;
	}
	

	

}
